package com.hoshiicloud.shxping.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.controller.BaseController;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.shxping.dto.ConsumerDto;
import com.hoshiicloud.shxping.dto.MemberAddressDto;
import com.hoshiicloud.shxping.feign.IUserFeignService;
import com.hoshiicloud.shxping.feign.IdGeneratorService;
import com.hoshiicloud.shxping.param.MemberAddressPageParam;
import com.hoshiicloud.shxping.param.MemberAddressUpdateParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * @author yangcheng
 * @version 1.0
 * @title: AddressController
 * @description: 会员收货地址相关接口Controller
 * @date 2019/6/3 10:08
 * @mail maiboyc@163.com
 */
@Slf4j
@RestController
@RequestMapping("/address")
@Api(value = "收货地址管理", tags = {"收货地址相关操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class AddressController extends BaseController {

    // ID生成微服务Service
    @Resource
    private IdGeneratorService idGeneratorService;

    // 用户中心微服务Service
    @Resource
    private IUserFeignService userFeignService;

    /**
     * 新增收货地址
     * @param entity 新增对象
     * @param result 验证结果
     * @return Response<Long>
     */
    @PostMapping("/addAddress")
    @ApiOperation(value = "新增收货地址", notes = "新增收货地址", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> addAddress(@RequestBody @Valid MemberAddressDto entity, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                Response<ConsumerDto> consumer = userFeignService.getConsumerById(entity.getUserId());
                if (null != consumer && consumer.getCode() == 200 && null != consumer.getData()) {
                    String id = idGeneratorService.queryGeneratorId();
                    entity.setId(Long.parseLong(id));
                    entity.setCreatedby("admin");
                    entity.setCreatedtime(LocalDateTime.now());
                    entity.setUpdatedby("admin");
                    entity.setUpdatedtime(LocalDateTime.now());
                    entity.setDefAddress(0);
                    rs = userFeignService.addAddress(entity);
                } else {
                    log.error("新增收货地址异常，没有对应的会员信息！");
                    rs = Response.failed("新增收货地址异常，没有对应的会员信息！");
                }
            }
        } catch (Exception e) {
            log.error("新增收货地址异常"+"["+e.getMessage()+"]");
            rs = Response.failed("新增收货地址异常"+e.getMessage());
        }
        return rs;
    }

    /**
     * 删除收货地址
     * @param id ID
     * @return Response<Long>
     */
    @DeleteMapping("/deleteAddress/{id}")
    @ApiOperation(value = "删除收货地址", notes = "删除收货地址", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> deleteAddress(@PathVariable("id") Long id) {
        Response<Long> rs;
        try {
            rs = userFeignService.deleteAddress(id);
        } catch (Exception e) {
            log.error("删除收货地址异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 修改收货地址
     * @param entity 修改对象
     * @param result 验证结果
     * @return Response<Long>
     */
    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改收货地址", notes = "修改收货地址", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> updateAddress(@RequestBody MemberAddressUpdateParam entity, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                Response<ConsumerDto> consumer = userFeignService.getConsumerById(entity.getUserId());
                if (null != consumer && consumer.getCode() == 200 && null != consumer.getData()) {
                    entity.setUpdatedby("admin");
                    entity.setUpdatedtime(LocalDateTime.now());
                    entity.setDefAddress(0);
                    rs = userFeignService.updateAddress(entity);
                } else {
                    log.error("修改收货地址异常，没有对应的会员信息！");
                    rs = Response.failed("修改收货地址异常，没有对应的会员信息！");
                }
            }
        } catch (Exception e) {
            log.error("修改收货地址异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    /**
     * 查看收货地址
     * @param param 查询参数
     * @return Response<IPage<MemberAddressDto>>
     */
    @PostMapping("/viewAddress")
    @ApiOperation(value = "查看收货地址", notes = "查看收货地址", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Page<MemberAddressDto>> viewAddress(@RequestBody MemberAddressPageParam param) {
        Response<Page<MemberAddressDto>> rs;
        try {
            rs = userFeignService.viewAddress(param);
        } catch (Exception e) {
            log.error("查看收货地址异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

}
